package com.toutiao

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{ServletContextHandler,ServletHolder}

import com.toutiao.score.ExpMeasure
import com.toutiao.score.ScoreFeaturer
import com.toutiao.score.MatchFeaturer
import com.toutiao.score.SimScorer
import com.toutiao.score.SimServlet
import com.toutiao.score.ExpServlet

import org.apache.log4j.Logger

class ScorerServer(prop:AppProp) {
  
  val _log = Logger.getLogger(this.getClass.getName)
  
  def run = {
    
    _log.info("Get Measure")
    
    val m = ExpMeasure.get(prop("LOCATION_MEASURE"))
    val n = prop("DOC_NUM").toInt
    
    val fs1 = new ScoreFeaturer(m,n)
    val fs2 = new MatchFeaturer()
    
    val ss = new SimScorer(fs1,fs2)
    
    
    _log.info("Get Measure Done")
    
    val server = new Server(prop("SERVER_PORT").toInt)
    val root = new ServletContextHandler(server, "")
    
    val s1 = new ExpServlet(fs1)
    val s2 = new SimServlet(ss)

    root.addServlet(new ServletHolder(s1),"/score")
    root.addServlet(new ServletHolder(s2),"/sim")
    
    try {
      server.start
    }
    catch {
      case e: Exception => {
        e.printStackTrace
        sys.exit
      }
    }    
    
  }

}
